//
//  NSDate+ZQDate.m
//  柳州微博
//
//  Created by Apple_Lzzy45 on 16/10/29.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "NSDate+ZQDate.h"

@implementation NSDate (ZQDate)
/**
 *  判断某个时间是否是今年
 *
 *  @return <#return value description#>
 */
-(BOOL)isThisYear//:(NSDate *)date
{
    //日历对象 方便两个日期差距
    NSCalendar *calendar =[NSCalendar currentCalendar];
    //获取微博创建日期的年份  //今年  yyyy1=yyyy2
    NSDateComponents *created_at_DC =[calendar components:NSCalendarUnitYear fromDate:self];
    
    NSDate *now =[NSDate date];
    //获取当前日期的年份
    NSDateComponents *nowCom =[calendar components:NSCalendarUnitYear fromDate:now];
    return created_at_DC.year ==nowCom.year;
}
/**
 *  判断是不是今天
 *
 */
-(BOOL)isToday//:(NSDate *)date
{
    //日历对象 方便两个日期差距
    NSCalendar *calendar =[NSCalendar currentCalendar];
    //获取微博创建日期的年份  //今年  yyyy1=yyyy2
    NSCalendarUnit  unit=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitDay;
    
    NSDateComponents *created_at_DC =[calendar components:unit fromDate:self];
    
    NSDate *now =[NSDate date];
    //获取当前日期的年份
    NSDateComponents *nowCom =[calendar components:unit fromDate:now];
    //return created_at_DC.day == nowCom.day;
    return (created_at_DC.year==nowCom.year)&&(created_at_DC.month==nowCom.month)&&(created_at_DC.day==nowCom.day);
}
/**
 *  判断是不是昨天
 *
 */
-(BOOL)isYesterday//:(NSDate *)date
{
    //创建日期格式
    NSDateFormatter *fmt =[[NSDateFormatter alloc]init];
    fmt.dateFormat=@"yyyy-MM-dd";
    //日历对象 方便两个日期差距
    NSCalendar *calendar =[NSCalendar currentCalendar];
    
    //获取微博创建日期的年份  //今年  yyyy1=yyyy2
    NSCalendarUnit  unit=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitDay;
    
    //  NSDateComponents *created_at_DC =[calendar components:unit fromDate:date];
    
    //创建当前时间
    NSDate *now =[NSDate date];
    
    //获取当前日期的年份
    //  NSDateComponents *nowCom =[calendar components:unit fromDate:now];
    //对比两个日期的差距
//    NSDateComponents *cmps =[calendar components:unit fromDate:self toDate:now options:0];
//    cmps =[calendar components:unit fromDate:self toDate:now options:0];
    
     NSDateComponents *created_at_DC =[calendar components:unit fromDate:self];
     NSDateComponents *nowCom =[calendar components:unit fromDate:now];
    
    return (created_at_DC.year==nowCom.year)&&(created_at_DC.month==nowCom.month)&&(created_at_DC.day==nowCom.day-1);
//    return (cmps.year ==0)&&(cmps.month==0)&&(cmps.day == -1);
}

/**
 *  判断时间差
 */
-(NSDateComponents *)dataCha
{
    NSDateFormatter *fmt =[[NSDateFormatter alloc]init];
    //    _created_at =@"Sun Oct 23 14:54:51 +0800 2014";
    fmt.dateFormat =@"EEE MMM dd HH:mm:ss Z yyyy";
    
    //获取当前时间
    NSDate *now =[NSDate date];
    
    //日历对象 方便两个日期差距
    NSCalendar *calendar =[NSCalendar currentCalendar];
    
    //日历的单元
    NSCalendarUnit  unit=NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    //计算两个日期的时间差值
    NSDateComponents *cmps =[calendar components:unit fromDate:self toDate:now options:0];
    
    return cmps;

}
@end
